package cn.chenfei.jfx.common;


import javafx.scene.Node;
import javafx.stage.Stage;
import javafx.stage.Window;

/**
 * @author ChenFei
 * @date 2022年4月8日
 */
public class DragUtil {

    private double xOffset;
    private double yOffset;

    private DragUtil() {

    }

    public static DragUtil create() {
        return new DragUtil();
    }

    /**
     * 绑定移动的窗口和移动的节点
     *
     * @param stage
     * @param node
     */
    public void move(final Window stage, final Node node) {
        node.setOnMousePressed(event -> {
            event.consume();
            xOffset = stage.getX() - event.getScreenX();
            yOffset = stage.getY() - event.getScreenY();
        });
        node.setOnMouseDragged(event -> {
            event.consume();
            stage.setX(event.getScreenX() + xOffset);
            stage.setY(event.getScreenY() + yOffset);
        });
    }
}